package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Payment;
import cn.wolfcode.crm.query.PaymentQueryObject;
import cn.wolfcode.crm.service.IPaymentService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

@Controller
@RequestMapping("/payment")
public class PaymentController {
    @Autowired
    private IPaymentService paymentService;

    @RequestMapping("")
    public String index(Model model,PaymentQueryObject qo){
        model.addAttribute("allAmount",paymentService.allAmount(qo));
        return "payment";
    }

    @RequestMapping("list")
    @ResponseBody
    public PageResult query( PaymentQueryObject qo){
        return paymentService.query(qo);
    }

    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Payment payment){
        JsonResult jsonResult=new JsonResult();
        try {
            paymentService.save(payment);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMsg("新增失败");
        }
        return jsonResult;
    }

    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Payment payment){
        JsonResult jsonResult=new JsonResult();
        try {
            paymentService.update(payment);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMsg("保存失败");
        }

        return jsonResult;
    }
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id){
        JsonResult jsonResult=new JsonResult();
        try {
            paymentService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMsg("保存失败");
        }

        return jsonResult;
    }

    //导出会员信息
    //导出表格
    @RequestMapping("export")
    @ResponseBody
    public void export(PaymentQueryObject qo, HttpServletResponse response) throws Exception {

        //1.查询出列表数据
        qo.setRows(10000);
        List pays = paymentService.queryForList(qo);

        response.setHeader("Content-Disposition", "attachment;filename=payment.xls");
        OutputStream outputStream = response.getOutputStream();

        //创建一个工作簿,目标是 outputStream
        WritableWorkbook workbook = Workbook.createWorkbook(outputStream);

        //2.处理标题
        //创建一个可以写 sheet ,
        WritableSheet sheet = workbook.createSheet("payment", 0);

        //创建一个单元格
        Label payclassifyNameLabel = new Label(0, 0, "支出分类");
        Label amountLabel = new Label(1, 0, "支出金额");
        Label inputTime = new Label(2, 0, "支出时间");
        Label employeeName = new Label(3, 0, "支出人员");
        Label remake = new Label(4, 0, "备注");

        sheet.addCell(payclassifyNameLabel);
        sheet.addCell(amountLabel);
        sheet.addCell(inputTime);
        sheet.addCell(employeeName);
        sheet.addCell(remake);

        //3.循环迭代list,每一个payment 对应表格中的一个行数据,二每一个employe的每一个字段,应该对应一行的某一个单元格
        for(int i = 0; i < pays.size() ;i++){
            Payment pay  = (Payment) pays.get(i);
            int row = i+1;
            Label tempPayclassifyNameLabel = new Label(0, row,pay.getPayclassify().toString());
            Label tempAmountLabel = new Label(1, row, pay.getAmount().toString());
            Label tempInputTimeLabel = new Label(2, row, pay.getInputTime().toLocaleString());
            Label tempEmployeeNameLabel = new Label(3, row, pay.getEmployee().getName());
            Label tempRemakeLabel = new Label(4, row, pay.getRemake());

            sheet.addCell(tempPayclassifyNameLabel);
            sheet.addCell(tempAmountLabel);
            sheet.addCell(tempInputTimeLabel);
            sheet.addCell(tempEmployeeNameLabel);
            sheet.addCell(tempRemakeLabel);
        }

        //4.下文件
        //把内容中的工作簿对象的相关内容写到 输出流 里面
        workbook.write();
        //关闭工作簿
        workbook.close();
    }

}
